package nfa;

import fa.FARule;
import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.List;
import java.util.Set;

import static org.junit.jupiter.api.Assertions.*;

/**
 * @author haoly
 * @title BalancedBracketTest
 * @date 2025/4/19 06:05
 * @description 平衡括号问题测试类
 */
public class BalancedBracketTest {
    /**
     * 测试4个状态的NFA只能处理嵌套层数小于等于3的平衡括号语言
     */
    @Test
    void testAccepts() {
        List<FARule<Integer>> rules = Arrays.asList(
                new FARule<>(0, '(', 1),
                new FARule<>(1, ')', 0),
                new FARule<>(1, '(', 2),
                new FARule<>(2, ')', 1),
                new FARule<>(2, '(', 3),
                new FARule<>(3, ')', 2)
        );

        NFARulebook<Integer> rulebook = new NFARulebook<>(rules);

        NFADesign<Integer> nfaDesign = new NFADesign<>(0, Set.of(0), rulebook);

        assertFalse(nfaDesign.accepts("(()"));

        assertFalse(nfaDesign.accepts("())"));

        assertTrue(nfaDesign.accepts("(())"));
        assertTrue(nfaDesign.accepts("(()(()()))"));
        //请注意："(((())))"的嵌套层数为4，显然是平衡括号语言，但该NFA判定不接受
//        assertFalse(nfaDesign.accepts("(((())))"));

    }
    /**
     * 测试5个状态的NFA只能处理嵌套层数小于等于4的平衡括号语言
     */
    @Test
    void testAccepts2() {
        List<FARule<Integer>> rules = Arrays.asList(
                new FARule<>(0, '(', 1),
                new FARule<>(1, ')', 0),
                new FARule<>(1, '(', 2),
                new FARule<>(2, ')', 1),
                new FARule<>(2, '(', 3),
                new FARule<>(3, ')', 2),
                new FARule<>(3, '(', 4),
                new FARule<>(4, ')', 3)
        );

        NFARulebook<Integer> rulebook = new NFARulebook<>(rules);

        NFADesign<Integer> nfaDesign = new NFADesign<>(0, Set.of(0), rulebook);

        assertTrue(nfaDesign.accepts("(((())))"));

    }
}
